package com.example.mylibrarybase.yumengmeng02.adapter;

import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.mylibrarybase.R;
import com.example.mylibrarybase.utils.LogUtils;
import com.example.mylibrarybase.yumengmeng02.bean.UploadFile_02;

import java.util.ArrayList;
import java.util.List;


/**
 * 展示图片的 adapter
 * Created by Administrator on 2016/5/11.
 *///
public class UploadPhotoAdapter_02 extends RecyclerView.Adapter<UploadPhotoAdapter_02.EnquiryViewHolder> {

    private static final int TYPE_CAMERA = 0;
    private static final int TYPE_PHOTO = 1;
    private Context c;
    private LayoutInflater mInflater;
    private Handler mHandler;
    //回调  号
    private MyItemLongClickListener mItemLongClickListener;
    private MyItemClickListener mItemClickListener;
    private int mPosition;

    // 回调 接口

    public interface MyItemClickListener {
        /**
         * 把 你点的 对象返回 回去
         *
         * @param view       你点的啥
         * @param uploadFiles 你点的图片的 本地路径 的集合
         * @param postion    你点的号
         */
         void onItemClick(View view, List<UploadFile_02> uploadFiles, int postion);
    }

    public interface MyItemLongClickListener {
         void onItemLongClick(View view, UploadFile_02 uploadFile, int postion);
    }

    /**
     * 设置Item点击监听
     *
     * @param listener
     */
    public void setOnItemClickListener(MyItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public void setOnItemLongClickListener(MyItemLongClickListener listener) {
        this.mItemLongClickListener = listener;
    }

    //图片的 数据 集合
    private List<UploadFile_02> uploadFiles = new ArrayList<UploadFile_02>();

    public UploadPhotoAdapter_02(Context context, Handler handler) {
        this.c = context;
        this.mInflater = LayoutInflater.from(context);
        this.mHandler = handler;
    }

    public void setData(List<UploadFile_02> uploadFiles) {
        this.uploadFiles.addAll(uploadFiles);
        notifyDataSetChanged();
    }

    public List<UploadFile_02> getUploadFiles() {
        return uploadFiles;
    }

    public void setUploadFiles(List<UploadFile_02> uploadFiles) {
        this.uploadFiles = uploadFiles;
    }

    @Override
    public EnquiryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            //如果点了 0 也就是 添加上传的加号 按钮
            case TYPE_CAMERA:
                View view = mInflater.inflate(R.layout.item_add_photo_layout_02, null);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mHandler.sendEmptyMessage(100);
                    }
                });
                return new EnquiryViewHolder(view);
            //如果点了 1 也就是 图片 他才有点击事件
            case TYPE_PHOTO:
                View view1 = mInflater.inflate(R.layout.photo_item_02, null);

                return new EnquiryViewHolder(view1, mItemClickListener, mItemLongClickListener);
        }
        return null;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1) {
            return TYPE_CAMERA;
        } else {
            return TYPE_PHOTO;
        }
    }

    @Override
    public void onBindViewHolder(EnquiryViewHolder holder, int position) {
        //  PhotoView photoView1;
        if (getItemViewType(position) != TYPE_CAMERA) {
            //图片  具体图片数据
            UploadFile_02 uploadFile = uploadFiles.get(position);
            LogUtils.i("uploadFile 图片数据 适配器的那个类" + uploadFile);

            Uri parse = Uri.parse("file://" + uploadFile.getFile_path());
            Glide
                    .with(c)
                    .load(parse)
                    .placeholder(R.drawable.ic_photo_loading_02) // can also be a drawable
                    .centerCrop()
                    .into(holder.item_order_facebook_item_sellerImg);
           // holder.item_order_facebook_item_sellerImg.setImageURI(parse);
            holder.progressBar.setVisibility(View.GONE);
            if (uploadFile.isUploadComplete()) {
                holder.progressBar.setVisibility(View.GONE);
            } else {
              //  uploadFile.startUpload(c, holder.progressBar);
            }

        }
    }


    @Override
    public int getItemCount() {
        return uploadFiles.size() + 1;
    }

    class EnquiryViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        private MyItemClickListener mListener;
        private MyItemLongClickListener mLongClickListener;

       // SimpleDraweeView simpleDraweeView;
        ProgressBar progressBar;
        TextView timeText;
        private ImageView item_order_facebook_item_sellerImg;

        public EnquiryViewHolder(View view, MyItemClickListener listener, MyItemLongClickListener longClickListener) {
            super(view);
            item_order_facebook_item_sellerImg = (ImageView) view.findViewById(R.id.item_order_facebook_item_sellerImg);
            progressBar = (ProgressBar) view.findViewById(R.id.progressBar);


            this.mListener = listener;
            this.mLongClickListener = longClickListener;
            view.setOnClickListener(this);
            view.setOnLongClickListener(this);
        }

        public EnquiryViewHolder(View view) {


            super(view);


            item_order_facebook_item_sellerImg = (ImageView) view.findViewById(R.id.item_order_facebook_item_sellerImg);

            progressBar = (ProgressBar) view.findViewById(R.id.progressBar);

        }

        @Override
        public void onClick(View v) {
            mPosition = getLayoutPosition();
            if (uploadFiles.size() >= 1) {

                mListener.onItemClick(v, uploadFiles, mPosition);
            } else {
                mListener.onItemClick(v, null, -1);
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (mLongClickListener != null) {
                mPosition = getLayoutPosition();

                mLongClickListener.onItemLongClick(v, uploadFiles.get(mPosition), mPosition);
            } else {
                mLongClickListener.onItemLongClick(v, null, -1);
            }
            return true;

        }
    }

}
